<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: pl125 <xskjs888@163.com>
// +----------------------------------------------------------------------

namespace api\wxapp\controller;

use api\wxapp\controller\ApiBaseController;
use think\Db;
use api\wxapp\model\ShopGoods;
use api\wxapp\model\Goods;
use api\wxapp\model\Order;
use api\wxapp\model\OrderList;
class OrderController extends ApiBaseController
{
    //提交订单
    public function index(Order $order)
    {
        //传参格式为 json
        $params = $this->getParam();
        $this->checkshopid($params);
        $this->checkuid($params);
        !isset($params['data']) ? $this->error('参数缺失!!!') : '';
        !isset($params['freight']) ? $this->error('参数缺失!!!') : '';
        !isset($params['pay_price']) ? $this->error('参数缺失!!!') : '';
        !isset($params['pay_type']) ? $this->error('参数缺失!!!') : '';
        !isset($params['is_coupon']) ? $this->error('参数缺失!!!') : '';
        $data = json_decode($params['data'],true);

        $insert['uid'] = $params['uid']; //用户id
        $insert['freight'] = $params['freight'];//运费
        $insert['pay_price'] = $params['pay_price'];//支付价格
        $insert['status'] = 1; //未支付状态
        $insert['addtime'] = date('Y-m-d H:i:s',time()); //未支付状态
        $insert['pay_type'] = $params['pay_type']; //支付方式
        $insert['shop_id'] = $params['shop_id'];
        $insert['type'] = 1;//1:常规订单2:积分订单3:秒杀订单
        if(isset($params['is_coupon'])){
            $insert['is_coupon'] = $params['is_coupon']; //是否使用了优惠券
        }
        if(isset($params['remark'])){
            $insert['remark'] = $params['remark']; //订单备注
        }
        $insert['ordernum'] = $this->getOrderNum($params['uid']);
        //开始事务
        Db::startTrans();
        try{
            $id = Order::insertGetId($insert);
            if(!$id){
                throw new \Exception('订单表储存失败');
            }
            //添加订单商品表
            $old_price = 0;
            $goods_price = 0;
            $datas = array();
            foreach($data as $k=>$v){
                $datas[$k]['goods_id'] = $v['goods_id'];
                //查询当前商品是否是店售的商品
                $find[$k] = ShopGoods::where(['goods_id'=>$v['goods_id'],'shop_id'=>$params['shop_id']])->find();
                if($find[$k]){
                    $datas[$k]['shop_id'] = $params['shop_id'];
                }else{
                    $datas[$k]['shop_id'] = 0;
                }
                $datas[$k]['num'] = $v['num'];
                $datas[$k]['price'] = $v['price'];
                $datas[$k]['para'] = $v['para'];
                $datas[$k]['order_id'] = $id;
                $datas[$k]['old_price'] = $v['price'];
                $old_price += $v['price'] * $v['num'];
                $datas[$k]['addtime'] = date('Y-m-d H:i:s',time());
                //查询商品的原来售卖价格
                $goods_price += $v['price'] * $v['num'];
            }
            $OrderList = new OrderList;
            $res = $OrderList->saveAll($datas);
            if(!$res){
                throw new \Exception('订单商品表储存失败');
            }
            //在更新下订单表的数据
            $res1 = Order::update(['id'=>$id,'old_price'=>$old_price,'goods_price'=>$goods_price]);
            if(!$res1){
                throw new \Exception('订单表更新失败');
            }
            $insert['id'] = $id;
            $insert['old_price'] = $old_price;
            $insert['goods_price'] = $goods_price;
            Db::commit(); //提交事务
        }catch(\Exception $e){
            Db::rollback(); //回滚事务
            $this->error($e->getMessage());
        }
        $this->success('提交成功!',$insert);
    }
}
